﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.SweptVolume.CApSamplingManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.AutomaticInterferenceVolumeCreation;
using System;


namespace DnProcessSimulateCommands.SweptVolume;

public abstract class CApSamplingManager
{
  private const bool DEFAULT_SHOULD_REWIND = true;
  protected CApSimulationHandler m_simHandler;
  protected CApSVSamplingObjectsHandler m_objHandler;
  protected bool m_includeCables;
  private bool m_simulationInProgress;
  private bool m_rewind;

  internal event LowLevelStepProgressEventHandler LowLevelStepProgress;

  public CApSamplingManager(
    CApSimulationHandler simHandler,
    CApSVSamplingObjectsHandler objHandler,
    bool includeCables)
  {
    this.m_simHandler = simHandler;
    this.m_objHandler = objHandler;
    this.m_includeCables = includeCables;
    this.m_simulationInProgress = false;
    this.m_rewind = true;
  }

  public bool Rewind
  {
    get => this.m_rewind;
    set => this.m_rewind = value;
  }

  public CApSimulationHandler SimulationHandler => this.m_simHandler;

  public void startSampling()
  {
    this.internalInitializeSampling();
    this.m_simHandler.init();
    this.m_objHandler.init();
    this.m_simHandler.samplingOccurenceReached += new samplingOccurenceReachedEventHandler(this.onSamplingOccurenceReached);
    this.m_objHandler.samplingOccurenceReached += new samplingOccurenceReachedEventHandler(this.onSamplingOccurenceReached);
    this.m_simulationInProgress = true;
    this.m_simHandler.startSimulation();
    this.onSimulationEnded();
  }

  public void stopSampling()
  {
    this.unregisterEvents();
    if (!this.m_simulationInProgress)
      return;
    this.m_simHandler.stopSimulation();
  }

  private void onSamplingOccurenceReached(object sender, EventArgs args)
  {
    if (this.m_simHandler.maySample() & this.m_objHandler.maySample())
    {
      this.m_simHandler.beforeSample();
      this.m_objHandler.beforeSample();
      this.sample();
      this.m_simHandler.afterSample();
      this.m_objHandler.afterSample();
    }
    if (this.m_simHandler.CurrentTime != Math.Round(this.m_simHandler.CurrentTime))
      return;
    this.fireLowLevelProgress();
  }

  private void onSimulationEnded()
  {
    this.unregisterEvents();
    this.m_simulationInProgress = false;
    this.finalizeSampling();
  }

  private void finalizeSampling()
  {
    this.internalFinalizeSampling();
    if (this.m_rewind)
      this.m_simHandler.rewind();
    this.m_simHandler.cleanup();
    this.m_objHandler.cleanup();
  }

  private void unregisterEvents()
  {
    this.m_simHandler.samplingOccurenceReached -= new samplingOccurenceReachedEventHandler(this.onSamplingOccurenceReached);
    this.m_objHandler.samplingOccurenceReached -= new samplingOccurenceReachedEventHandler(this.onSamplingOccurenceReached);
  }

  private void fireLowLevelProgress()
  {
    if (this.LowLevelStepProgress == null)
      return;
    this.LowLevelStepProgress((object) this, new EventArgs());
  }

  protected abstract void sample();

  protected virtual void internalInitializeSampling()
  {
  }

  protected virtual void internalFinalizeSampling()
  {
  }
}
